package edu.gatech.cs2340.bytebankers.presenter;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import edu.gatech.cs2340.bytebankers.model.Journey;
import edu.gatech.cs2340.bytebankers.model.Player;
import edu.gatech.cs2340.bytebankers.model.RiverCrossing;
import edu.gatech.cs2340.bytebankers.model.item.Item;
import edu.gatech.cs2340.bytebankers.notifications.GameEvents;
import edu.gatech.cs2340.bytebankers.notifications.Notifier;
import edu.gatech.cs2340.bytebankers.view.IRiverView;
import edu.gatech.cs2340.bytebankers.model.item.Ox;

public class RiverPresenter extends Notifier implements IRiverPresenter {

	Journey journey;
	IRiverView view;
	RiverCrossing river;
	
	public RiverPresenter(Journey journey) {
		this.journey = journey;
		river = (RiverCrossing)journey.getlastLocation();
		
	}
	
	@Override
	public void setView(IRiverView view) {
		this.view = view;
		view.setName(river.getName());
		view.setDepth(river.getDepth());
		view.setMoneyLeft(journey.getLeader().getMoneyLeft());
	}

	@Override
	public void onChoiceMade() {
		int choice = view.getChoice();
		
		if(choice == 1) {
			journey.getLeader().withdrawMoney(250);
		}
		
		boolean crossedSuccessfully;
		
		crossedSuccessfully = river.crossWasSuccessful(choice);
		
		Random rand = new Random();
		
		if(crossedSuccessfully) {
			view.showResult("You crossed successfully!");
		} else {
			
			boolean personDied =  (0==rand.nextInt(4));
			
			if(personDied) {
				int numPlayersLeft = journey.getPlayers().size();
				if(numPlayersLeft > 1) {
					// kill a party member -- not the leader
					for(int i = 1; i < journey.getPlayers().size(); i++) {
						int numNonLeaderPlayers = 
							journey.getPlayers().size() - 1;
						
						// add one to the end to exclude the leader
						int numPlayerToKill = 
							rand.nextInt(numNonLeaderPlayers) + 1; 
						
						Player playerToKill = 
							journey.getPlayers().get(numPlayerToKill);
						
						journey.killPlayer(playerToKill);
						
						view.showResult("Your wagon tipped, and " + 
								playerToKill.getName() + 
								" drowned. Sorry about that.");
					}
				} else {
					view.showResult("The leader has died.");
					// kill the leader, end the game
					sendNotification(GameEvents.LOSING);
				}
			} else {
				// wagon item is lost
				List<Item> items = journey.getWagon().getListOfAvailableItems();
				
				
				
				Item itemToLose;
				
				itemToLose = items.get(rand.nextInt(items.size()));
				
				int numItemsLeft = 
					journey.getWagon().getItemQuantityRemaining(itemToLose);
				
				int numItemsToUse = (int) Math.ceil(numItemsLeft / 2.0);
				
				journey.getWagon().useItem(itemToLose, numItemsToUse);
				
				if(
					itemToLose instanceof Ox && journey.getWagon().
							getItemQuantityRemaining(itemToLose) == 0) {
					// kill the leader, end the game
					view.showResult(
							"Your wagon tipped, and you lost your last Ox. " + 
							"That kinda sucks.");
					sendNotification(GameEvents.LOSING);
				} else {
					view.showResult(
							"Your wagon tipped, and you lost " + numItemsToUse + 
							" of your " + itemToLose.getName() +
							". Ouch.");
				}
			}
		}
	}

	@Override
	public void onShowResultClose() {
		this.sendNotification(GameEvents.RIVER_CROSSED);
	}

}
